#!/usr/bin/env bash

source /etc/profile

BASE_PATH="/home/jhd/"
BASE_PATH_REG=^/home/jhd/.*$
CONFIGS_PATH="/home/jhd/config/services/"
CONFIG_KEY_START='start'
CONFIG_KEY_STOP='stop'
CONFIG_KEY_LOGS='logs'
CONFIG_KEY_RETAIN_DAYS='retain_days'
CONFIG_KEY_LAST_CLEAR_DATE='last_clear_date'


function is_valid_config(){
    local __file_path=$1

    if  [ ! -n "${__file_path}" ]; then
        echo "__file_path not exist!"
        return 0
    fi
    get_by_key ${CONFIG_KEY_START} ${__file_path}
    local _start=__value_by_key
    get_by_key ${CONFIG_KEY_STOP} ${__file_path}
    local _stop=__value_by_key
    get_by_key ${CONFIG_KEY_LOGS} ${__file_path}
    local _logs=__value_by_key
    get_by_key ${CONFIG_KEY_LAST_CLEAR_DATE} ${__file_path}
    local _last_clear_date=__value_by_key

    if  [ ! -n "${_logs}" ]; then
        echo "__logs not exist!"
        return 0
    fi
    return 1
}

function get_by_key(){
    local __key=$1
    local __file=$2
    __value_by_key=''
    if  [ ! -n "${__key}" ]; then
        echo "__key not exist!"
        return 0
    fi

    if  [ ! -n "${__file}" ]; then
        echo "__file not exist!"
        return 0
    fi
    local __reg="/^${__key}=/!d;s/.*=//"
    __value_by_key=`sed ${__reg} ${__file}`
    return 1
}

function is_require_clear(){
    local __days=$1
    local __last_clear_date=$2

    if  [ ! -n "${__last_clear_date}" ]; then
        echo "__last_clear_date not exist!"
        return 1
    fi

    if  [ ! -n "${__days}" ] || [ "${__days}" -lt 1 ]; then
        echo "__days not exist!"
        return 1
    fi
    local __now_date=$(date +%Y-%m-%d)
    local __now_date_timestamp=$(date -d "${__now_date}" +%s)
    local __last_date_timestamp=$(date -d "${__last_clear_date}" +%s)
    local __day_difference_s=`expr ${__now_date_timestamp} - ${__last_date_timestamp}`
    local __day_difference_day=`expr ${__day_difference_s} / 86400`;
    echo "days${__day_difference_day}"
    if [ "${__day_difference_day}" -ge "${__days}" ]; then
        return 1
    fi
    return 0
}

function change_clear_date(){
    local __file=$1
    if  [ ! -n "${__file}" ] || [ ! -f "${__file}" ]; then
        echo "__file ${__file} not exist!"
        return 0
    fi

    local __now_date=$(date +%Y-%m-%d)
    sed -i "s/^\(last_clear_date*=\s*\).*$/\1${__now_date}/" ${__file}
    return 1
}

function is_lock(){
    __file_path=$1
    if  [ -n "$(fuser -v ${__file_path})" ]; then
        return 1
    fi
    return 0
}

function clear_logs(){
    local __path=$1
    if [[ !${__path} =~ ^\/home\/jhd\/.*$ ]]; then
        echo "folder ${__path} must in ${BASE_PATH}"
        exit 0
    fi

    if [ -d "${__path}" ];  then
            local __file_list=$(ls ${__path})
            for __file in ${__file_list}
            do
               local _re_file_path="${__path}/${__file}"
               local _path_name=__file
                if [ -d "${_re_file_path}" ];  then
                    echo "${_path_name} is a directory"
                    #recursion
                    clear_logs ${_re_file_path}
                else [ -f "${__file}" ]
                    echo "${_re_file_path} is a file"
                    is_lock ${_re_file_path}
                    local __is_lock=$?
                    if [ 1 -eq "${__is_lock}" ]; then
                        echo "${_re_file_path} is locked"
                        echo "" > ${_re_file_path}
                     else
#                        echo "${_re_file_path} is not locked"
                        rm -f ${_re_file_path}
                    fi
                fi
            done
    else [ -f "${__path}" ]
        local _path_name="${__path%.*}"
        is_lock ${__path}
        local __is_lock=$?
        if [ 1 -eq "${__is_lock}" ]; then
            echo "${_path_name} is locked"
            echo "" > ${__path}
         else
#            echo "${_path_name} is not locked"
            rm -f ${__path}
        fi
    fi


}

function service_restart(){
    local __stop=$1
    local __start=$2

    if  [ ! -n "${__start}" ]; then
        echo "ERR! _start not exist!"
        return 0
    fi
    if  [ ! -n "${__stop}" ]; then
        echo "ERR! __sotp not exist!"
        return 0
    fi

    sh ${__stop}
    sh ${__start}
}

if [ ! -x "${CONFIGS_PATH}" ]; then
    echo "ERR! config folder not exist!"
    exit 0
fi

_file_list=$(ls ${CONFIGS_PATH})
if  [ ! -n "${_file_list}" ]; then
    echo "ERR! config file not exist!"
    exit 0
fi

for _file in ${_file_list}
do
    _file_name="${_file%.*}"
	if [ -d "${_file}" ];  then
        echo "ERR! ${_file_name} is a directory"
    else [ -f "${_file}" ]
        _file_path="${CONFIGS_PATH}${_file}"
        get_by_key ${CONFIG_KEY_START} ${_file_path}
        _start=${__value_by_key}
        get_by_key ${CONFIG_KEY_STOP} ${_file_path}
        _sotp=${__value_by_key}
        get_by_key ${CONFIG_KEY_LOGS} ${_file_path}
        _logs=${__value_by_key}
        get_by_key ${CONFIG_KEY_LAST_CLEAR_DATE} ${_file_path}
        _last_clear_date=${__value_by_key}
        get_by_key ${CONFIG_KEY_RETAIN_DAYS} ${_file_path}
        _retain_days=${__value_by_key}

        is_valid_config ${_file_path}
        _is_valid_config=$?
        if [ 0 -eq "${_is_valid_config}" ]; then
            echo "ERR! ${_file_name} not valid"
            continue
        fi
        is_require_clear ${_retain_days} ${_last_clear_date}
        __is_require_clear=$?
        if [ "${__is_require_clear}" -eq 0 ]; then
#            echo "${_file_name} not require clear"
            continue
        fi
        clear_logs ${_logs}
        service_restart ${_sotp} ${_start}
        change_clear_date ${_file_path}
    fi
done
exit 0